<?php
namespace App\Http\Services;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Model\Hint;
use App\Http\Model\User as UserModel;
use App\Http\Model\Device as DeviceModel;
use App\Modules\Popularize\Platform\PlatformKuaishou;
use App\Modules\Popularize\Platform\PlatformReportVivo;

//渠道平台上报相关
class PlatformReportServices extends CommonServices
{
    protected $deviceModel;
    protected $userModel;
    protected $thirdShumei;
    protected $userServices;

    public function __construct()
    {
        parent::__construct();

        $this->userModel = new UserModel();
        $this->deviceModel = new DeviceModel();
    }

    //广告归因，上报激活
    public function reportActivation($logId,$tomorrow=0)
    {
        do {
            if(empty($logId)) {
                break;
            }
            //获取渠道推广记录
            $log = $this->deviceModel->getChannelLog($logId);
            if(empty($log)) {
                break;
            }
            //渠道平台判断
            if($log['platform'] == 'vivo-juli') {//vivo聚力
                $service = new PlatformReportVivo($log, 0);
            } elseif ($log['platform'] == 'huawei-hengli') {//华为亨利

            } elseif($log['platform'] == 'kuaishou-yunhong') {//快手韵洪
                $service = new PlatformKuaishou($log,0);
            }else{
                break;
            }
            //次日留存上报
            if($tomorrow==1)
            {
                $service->actviteEvent(1);
            }else{
                $service->actviteEvent();
            }
        } while (false);
    }

    //广告归因，上报注册
    public function reportRegister($guid, $userId)
    {
        do {
            if(empty($guid) || empty($userId)) {
                break;
            }
            //获取渠道推广记录
            $log = $this->deviceModel->getChannelLogByGuid($guid);
            if(empty($log)) {
                break;
            }
            if(!empty($log['user_id'])) {
                break;
            }
            //绑定关系
            $this->deviceModel->updateChannelLog($log['id'],['user_id'=>$userId]);

            //渠道平台判断
            if($log['platform'] == 'vivo-juli') {//vivo聚力
                $service = new PlatformReportVivo($log, $userId);
            } elseif ($log['platform'] == 'huawei-hengli') {//华为亨利

            } elseif($log['platform'] == 'kuaishou-yunhong') {//快手韵洪
                $service = new PlatformKuaishou($log,$userId);
            }else {
                break;
            }

            $service->registerEvent();

        } while (false);
    }

    //广告归因，上报付费
    public function reportPay($userId, $amount)
    {
        do {
            if(empty($userId) || empty($amount)) {
                break;
            }
            //获取渠道推广记录
            $log = $this->deviceModel->getChannelLogByuserId($userId);
            if(empty($log)) {
                break;
            }
            if(empty($log['guid']) || empty($log['platform'])) {
                break;
            }

            //渠道平台判断
            if($log['platform'] == 'vivo-juli') {//vivo聚力
                $service = new PlatformReportVivo($log, $userId);
            } elseif ($log['platform'] == 'huawei-hengli') {////华为亨利

            } elseif($log['platform'] == 'kuaishou-yunhong') {//快手韵洪
                $service = new PlatformKuaishou($log,$userId);
            } else {
                break;
            }

            $service->payEvent($amount);

        } while (false);
    }


}
